<template>
  <div class="chat-container">
    <div class="tech-background">
      <div class="matrix-grid">
        <div v-for="n in 100" :key="n" class="grid-cell"></div>
      </div>
      <div class="floating-particles">
        <div v-for="n in 30" :key="n" class="particle"></div>
      </div>
    </div>
    
    <div class="chat-header">
      <el-button @click="$router.push('/')" type="text" class="back-btn">
        <el-icon><ArrowLeft /></el-icon> 返回
      </el-button>
      <div class="header-content">
        <div class="title-section">
          <el-icon class="tech-icon"><Monitor /></el-icon>
          <h2>AI 超级智能体</h2>
        </div>
        <div class="status-indicator">
          <div class="status-dot"></div>
          <span>系统在线</span>
        </div>
      </div>
    </div>
    
    <div class="chat-messages" ref="messagesContainer">
      <div v-for="(msg, index) in messages" :key="index" 
           :class="['message', msg.type === 'user' ? 'message-user' : 'message-ai']">
        <div class="message-content" v-html="renderMarkdown(msg.content)"></div>
      </div>
    </div>

    <div class="chat-input">
      <el-input
        v-model="inputMessage"
        type="textarea"
        :rows="3"
        placeholder="输入你的问题，我将为你提供智能解答..."
        @keyup.enter.ctrl="sendMessage"
        class="tech-input"
      />
      <el-button type="primary" @click="sendMessage" :loading="loading" class="send-btn">
        <el-icon><Promotion /></el-icon>
        发送
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { createSuperAgentChat } from '../api'
import { ArrowLeft, Monitor, Promotion } from '@element-plus/icons-vue'

const messages = ref([])
const inputMessage = ref('')
const loading = ref(false)
const messagesContainer = ref(null)

const scrollToBottom = async () => {
  await nextTick()
  if (messagesContainer.value) {
    messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
  }
}

// 简单的Markdown渲染函数
const renderMarkdown = (content) => {
  if (!content) return ''
  
  return content
    // 处理图片 ![alt](url)
    .replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1" class="chat-image" />')
    // 处理粗体 **text**
    .replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
    // 处理换行
    .replace(/\n/g, '<br>')
    // 处理列表
    .replace(/^(\d+)\.\s+/gm, '<span class="list-number">$1.</span> ')
}

const sendMessage = async () => {
  if (!inputMessage.value.trim() || loading.value) return

  const message = inputMessage.value
  messages.value.push({ type: 'user', content: message })
  inputMessage.value = ''
  loading.value = true
  
  await scrollToBottom()

  const eventSource = createSuperAgentChat(message)
  
  let aiResponse = ''
  
  eventSource.onmessage = (event) => {
    const chunk = event.data
    if (aiResponse === '') {
      messages.value.push({ type: 'ai', content: chunk })
    } else {
      messages.value[messages.value.length - 1].content += chunk
    }
    aiResponse += chunk
    scrollToBottom()
  }

  eventSource.onerror = () => {
    eventSource.close()
    loading.value = false
  }
}

onMounted(() => {
  // 可以在这里添加欢迎消息
  messages.value.push({ 
    type: 'ai', 
    content: '🤖 你好！我是AI超级智能体，拥有强大的计算能力和丰富的知识库。无论是技术问题、学术研究还是日常咨询，我都能为你提供专业的解答和建议。请告诉我你需要什么帮助？' 
  })
})
</script>

<style scoped>
.chat-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.tech-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #0c0c0c, #1a1a2e, #16213e);
  z-index: 0;
}

.matrix-grid {
  position: absolute;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(20, 1fr);
  grid-template-rows: repeat(20, 1fr);
  opacity: 0.1;
}

.grid-cell {
  border: 1px solid rgba(0, 255, 255, 0.1);
  animation: pulse 4s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.1; }
  50% { opacity: 0.3; }
}

.floating-particles {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.particle {
  position: absolute;
  width: 4px;
  height: 4px;
  background: #00ffff;
  border-radius: 50%;
  animation: float-particle 8s infinite linear;
  box-shadow: 0 0 10px #00ffff;
}

@keyframes float-particle {
  0% {
    transform: translateY(100vh) translateX(0);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-100px) translateX(100px);
    opacity: 0;
  }
}

.chat-header {
  padding: 20px;
  background: rgba(26, 26, 46, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  gap: 20px;
  position: relative;
  z-index: 1;
  border-bottom: 1px solid rgba(0, 255, 255, 0.2);
}

.back-btn {
  color: #00ffff;
  font-weight: 500;
}

.header-content {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.tech-icon {
  font-size: 24px;
  color: #00ffff;
}

h2 {
  margin: 0;
  color: #ffffff;
  font-size: 1.8rem;
  font-weight: 600;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #00ffff;
  font-size: 0.9rem;
  background: rgba(0, 255, 255, 0.1);
  padding: 6px 12px;
  border-radius: 20px;
  border: 1px solid rgba(0, 255, 255, 0.3);
}

.status-dot {
  width: 8px;
  height: 8px;
  background: #00ff00;
  border-radius: 50%;
  animation: blink 2s infinite;
}

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0.3; }
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 15px;
  position: relative;
  z-index: 1;
}

.message {
  max-width: 70%;
  padding: 15px 20px;
  border-radius: 20px;
  word-break: break-word;
  position: relative;
  backdrop-filter: blur(10px);
}

.message-ai {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(0, 255, 255, 0.2);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  color: #ffffff;
}

.message-user {
  align-self: flex-end;
  background: linear-gradient(135deg, #00ffff, #0080ff);
  color: #fff;
  box-shadow: 0 4px 15px rgba(0, 255, 255, 0.3);
}

.message-content {
  line-height: 1.6;
}

.message-content :deep(.chat-image) {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  margin: 10px 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(0, 255, 255, 0.2);
}

.message-content :deep(.list-number) {
  font-weight: bold;
  color: #00ffff;
  margin-right: 8px;
}

.message-content :deep(strong) {
  font-weight: bold;
}

.chat-input {
  padding: 20px;
  background: rgba(26, 26, 46, 0.95);
  backdrop-filter: blur(10px);
  display: flex;
  gap: 15px;
  position: relative;
  z-index: 1;
  border-top: 1px solid rgba(0, 255, 255, 0.2);
}

.tech-input {
  flex: 1;
}

.tech-input :deep(.el-textarea__inner) {
  resize: none;
  border-radius: 20px;
  border: 2px solid rgba(0, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.1);
  color: #ffffff;
  transition: all 0.3s ease;
}

.tech-input :deep(.el-textarea__inner:focus) {
  border-color: #00ffff;
  box-shadow: 0 0 0 3px rgba(0, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.15);
}

.tech-input :deep(.el-textarea__inner::placeholder) {
  color: rgba(255, 255, 255, 0.6);
}

.send-btn {
  background: linear-gradient(135deg, #00ffff, #0080ff);
  border: none;
  border-radius: 20px;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

.send-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 255, 255, 0.4);
}

.chat-input :deep(.el-textarea__inner) {
  resize: none;
}

/* 为浮动粒子设置随机位置和延迟 */
.particle:nth-child(1) { left: 10%; animation-delay: 0s; }
.particle:nth-child(2) { left: 20%; animation-delay: 1s; }
.particle:nth-child(3) { left: 30%; animation-delay: 2s; }
.particle:nth-child(4) { left: 40%; animation-delay: 3s; }
.particle:nth-child(5) { left: 50%; animation-delay: 4s; }
.particle:nth-child(6) { left: 60%; animation-delay: 5s; }
.particle:nth-child(7) { left: 70%; animation-delay: 0.5s; }
.particle:nth-child(8) { left: 80%; animation-delay: 1.5s; }
.particle:nth-child(9) { left: 90%; animation-delay: 2.5s; }
.particle:nth-child(10) { left: 15%; animation-delay: 3.5s; }
.particle:nth-child(11) { left: 25%; animation-delay: 4.5s; }
.particle:nth-child(12) { left: 35%; animation-delay: 5.5s; }
.particle:nth-child(13) { left: 45%; animation-delay: 1.2s; }
.particle:nth-child(14) { left: 55%; animation-delay: 2.2s; }
.particle:nth-child(15) { left: 65%; animation-delay: 3.2s; }
.particle:nth-child(16) { left: 75%; animation-delay: 4.2s; }
.particle:nth-child(17) { left: 85%; animation-delay: 5.2s; }
.particle:nth-child(18) { left: 95%; animation-delay: 0.8s; }
.particle:nth-child(19) { left: 5%; animation-delay: 1.8s; }
.particle:nth-child(20) { left: 85%; animation-delay: 2.8s; }
.particle:nth-child(21) { left: 12%; animation-delay: 3.8s; }
.particle:nth-child(22) { left: 22%; animation-delay: 4.8s; }
.particle:nth-child(23) { left: 32%; animation-delay: 5.8s; }
.particle:nth-child(24) { left: 42%; animation-delay: 1.5s; }
.particle:nth-child(25) { left: 52%; animation-delay: 2.5s; }
.particle:nth-child(26) { left: 62%; animation-delay: 3.5s; }
.particle:nth-child(27) { left: 72%; animation-delay: 4.5s; }
.particle:nth-child(28) { left: 82%; animation-delay: 5.5s; }
.particle:nth-child(29) { left: 92%; animation-delay: 0.3s; }
.particle:nth-child(30) { left: 8%; animation-delay: 1.3s; }
</style>